<?php

// Widget Class
class qns_adverts_widget extends WP_Widget {


/* ------------------------------------------------
	Widget Setup
------------------------------------------------ */

	function qns_adverts_widget() {
		$widget_ops = array( 'classname' => 'adverts_widget', 'description' => __('Display Adverts', 'qns') );
		$control_ops = array( 'width' => 600, 'height' => 200, 'id_base' => 'adverts_widget' );
		$this->WP_Widget( 'adverts_widget', __('Custom Adverts Widget', 'qns'), $widget_ops, $control_ops );
	}


/* ------------------------------------------------
	Display Widget
------------------------------------------------ */
	
	function widget( $args, $instance ) {
		extract( $args );

		echo $before_widget; 
					
			if ( $instance['advert_field'] ) {
				echo do_shortcode($instance['advert_field']);
			} 

		echo $after_widget;
	}	
	
	
/* ------------------------------------------------
	Update Widget
------------------------------------------------ */
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['advert_field'] = strip_tags( $new_instance['advert_field'] );
		return $instance;
	}
	
	
/* ------------------------------------------------
	Widget Input Form
------------------------------------------------ */
	 
	function form( $instance ) {
		$defaults = array(
		'advert_field' => '[advert_wrapper]
[advert image_url="http://website.com/image.jpg" link_url="http://website.com"]
[advert image_url="http://website.com/image.jpg" link_url="http://website.com"]
[advert image_url="http://website.com/image.jpg" link_url="http://website.com"]
[advert image_url="http://website.com/image.jpg" link_url="http://website.com"]
[/advert_wrapper]'
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
				
		<p>
			<label for="<?php echo $this->get_field_id( 'advert_field' ); ?>"><?php _e('Advert Code:', 'qns'); ?></label>
			<textarea rows="10" cols="30" class="widefat" id="<?php echo $this->get_field_id( 'advert_field' ); ?>" name="<?php echo $this->get_field_name( 'advert_field' ); ?>"><?php echo $instance['advert_field']; ?></textarea>
		</p>
		
	<?php
	}	
	
}

// Add widget function to widgets_init
add_action( 'widgets_init', 'qns_adverts_widget' );

// Register Widget
function qns_adverts_widget() {
	register_widget( 'qns_adverts_widget' );
}